﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Administrator
{
    public partial class AdminRegionsUI : System.Web.UI.Page
    {

        public Boolean IsVN = true;
        private String languageCode = "vi-VN";       
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private Int32 RegionID = HangSo.DEFAULT_ID;   

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserName"] == null)
                    Response.Redirect("../Account/EHTLogin.aspx");
                else
                {
                    try
                    {
                        languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                        if (languageCode == "en-us")
                        {
                            IsVN = false;
                        }

                    }
                    catch (Exception ex)
                    {
                    }
                    if (Request.QueryString["RegionID"] != null)
                        RegionID = Int32.Parse(Request.QueryString["RegionID"].ToString());
                    else
                        RegionID = HangSo.DEFAULT_ID;

                    if (Request.QueryString["Message"] != null)
                        ltrSuccess.Text = HelpMethod.ApplySuccess(Request.QueryString["Message"].ToString());

                    Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                    LoadRegion(RegionID);
                }
            }
        }


        public void LoadRegion(Int32 RegionID)
        {
            if (RegionID != -1)
            {
                ViewState["action"] = HangSo.ACTION_UPDATE;
                ViewState["ID"] = ID.ToString();
                if (IsVN == true)
                    lbAdd_Edit.Text = HangSo.Edit_VN;
                else
                    lbAdd_Edit.Text = HangSo.Edit_Other;
               
                Region theRegion = RegionProvider.GetRegionByRegionID(RegionID);
                this.txtDescription.Text = theRegion.Description;
                this.txtRegionCode.Text = theRegion.RegionCode;
                this.txtRegionName.Text = theRegion.RegionName;              
                this.txtServiceCost.Text = theRegion.ServiceCost.ToString();

                ViewState["ID"] = theRegion.RegionID;
                ViewState["action"] = HangSo.ACTION_UPDATE;
            }
            else
            {
                ViewState["action"] = HangSo.ACTION_ADD;
                if (IsVN == true)
                    lbAdd_Edit.Text = HangSo.Add_VN;
                else
                    lbAdd_Edit.Text = HangSo.Add_Other;
            }
        }

        private Boolean ValidateData()
        {
            String message = "";
            Boolean isSuccess = true;

            try
            {
                float value = float.Parse(this.txtServiceCost.Text.Trim());
            }
            catch (Exception ex)
            {
                isSuccess = false;
                message += "Tjenesten Cost må er nummer";
            }

            if (!isSuccess)
                ltrMessage.Text = message;

            return isSuccess;
        }

        protected void ImgSave_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();

            if (!ValidateData())
                return;

            Region theRegion = new Region();
            theRegion.Description = this.txtDescription.Text.Trim();
            theRegion.RegionCode = this.txtRegionCode.Text;
            theRegion.RegionName = this.txtRegionName.Text;

            if (action == HangSo.ACTION_ADD)
            {
                theRegion.RegionID = -1;
                RegionProvider.UIRegion(theRegion, true);
            }
            else
            {
                theRegion.RegionID = Int32.Parse(ViewState["ID"].ToString());
                RegionProvider.UIRegion(theRegion, false);
            }

            Response.Redirect("AdminRegions.aspx");
        }

        protected void ImgApply_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            Int32 RegionIDOut;
            if (!ValidateData())
                return;

            Region theRegion = new Region();
            theRegion.Description = this.txtDescription.Text.Trim();
            theRegion.RegionCode = this.txtRegionCode.Text;
            theRegion.RegionName = this.txtRegionName.Text;

            if (action == HangSo.ACTION_ADD)
            {
                theRegion.RegionID = -1;
                RegionIDOut = RegionProvider.UIRegion(theRegion, true);
            }
            else
            {
                theRegion.RegionID = Int32.Parse(ViewState["ID"].ToString());
                RegionIDOut = RegionProvider.UIRegion(theRegion, false);
            }

            Response.Redirect("AdminRegionsUI.aspx?RegionID=" + RegionIDOut.ToString() + "&&Message=" + HangSo.App_VN);
        }
    }
}